/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderLog;
import com.boxin.ims.modules.jiujiudevice.dao.JiuOrderLogDao;

/**
 * 订单明细Service
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuOrderLogService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuOrderLogService.class);
	
	@Autowired
	private JiuOrderLogDao jiuOrderLogDao;
	
	public JiuOrderLog get(Long id) {
		return jiuOrderLogDao.findOne(id);
	}
	
	public JiuOrderLog findByOrderNo(String orderNo) {
		DetachedCriteria dc = jiuOrderLogDao.createDetachedCriteria();
		dc.add(Restrictions.eq("orderNo", orderNo));
		dc.addOrder(Order.desc("id"));
		List<JiuOrderLog> list = jiuOrderLogDao.find(dc);
		if(list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}
	
	public JiuOrderLog findByOrderId(Long orderId) {
		DetachedCriteria dc = jiuOrderLogDao.createDetachedCriteria();
		dc.add(Restrictions.eq("orderId", orderId));
		dc.addOrder(Order.desc("id"));
		List<JiuOrderLog> list = jiuOrderLogDao.find(dc);
		if(list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}
	public List<JiuOrderLog> find(JiuOrderLog jiuOrderLog) {
		DetachedCriteria dc = jiuOrderLogDao.createDetachedCriteria();
		if (jiuOrderLog.getId()!=null && jiuOrderLog.getId()>0){
			dc.add(Restrictions.eq("id", jiuOrderLog.getId()));
		}
		if (jiuOrderLog.getOrderId()!= null){
			dc.add(Restrictions.eq("orderId", jiuOrderLog.getOrderId()));
		}
		if (StringUtils.isNotEmpty(jiuOrderLog.getDeviceSn())){
			dc.add(Restrictions.eq("deviceSn", jiuOrderLog.getDeviceSn()));
		}
		if (StringUtils.isNotEmpty(jiuOrderLog.getOrderNo())){
			dc.add(Restrictions.eq("orderNo", jiuOrderLog.getOrderNo()));
		}
		dc.addOrder(Order.desc("id"));
		return jiuOrderLogDao.find(dc);
	}
	public Page<JiuOrderLog> find(Page<JiuOrderLog> page, JiuOrderLog jiuOrderLog) {
		DetachedCriteria dc = jiuOrderLogDao.createDetachedCriteria();
		if (jiuOrderLog.getId()!=null && jiuOrderLog.getId()>0){
			dc.add(Restrictions.eq("id", jiuOrderLog.getId()));
		}
		if (jiuOrderLog.getOrderId()!= null){
			dc.add(Restrictions.eq("orderId", jiuOrderLog.getOrderId()));
		}
		if (StringUtils.isNotEmpty(jiuOrderLog.getDeviceSn())){
			dc.add(Restrictions.eq("deviceSn", jiuOrderLog.getDeviceSn()));
		}
		if (StringUtils.isNotEmpty(jiuOrderLog.getOrderNo())){
			dc.add(Restrictions.eq("orderNo", jiuOrderLog.getOrderNo()));
		}
		dc.addOrder(Order.desc("id"));
		return jiuOrderLogDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public JiuOrderLog save(JiuOrderLog jiuOrderLog) {
		return jiuOrderLogDao.save(jiuOrderLog);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuOrderLogDao.deleteById(id);
	}
	
}
